//
//  STFilterView.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/10/19.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STDoorFilterView.h"
#import "STTimerDayCell.h"
#import "SWServiceModel.h"

@implementation STDoorFilterView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    collectionFrame = frame;
    return self;
}

-(void)setHairerFilterView{
    arrRows = [NSMutableArray array];
    [PersonCenterNet getserviceListWithSuccess:^(NSURLSessionTask *task, id responseObject) {
        NSNumber *code = responseObject[@"code"];
        if (code.integerValue == 200) {

            arrRows = (NSMutableArray *)[NSArray modelArrayWithClass:[SWServiceModel class] json:responseObject[@"data"]];
            [self setFileterCollectionView];
        }
    } Failure:nil];

//    NSArray * arr = @[@"洗剪吹",@"洗吹",@"烫发",@"染发",@"护理",@"接发",@"洗色",@"拉直",@"接发",@"洗色",@"拉直"];
//    for (int i =0; i<arr.count; i++) {
//        SWServiceModel * model = [SWServiceModel new];
//        model.name = arr[i];
//        [arrRows addObject:model];
//    }
//    [self setFileterCollectionView];
}

-(void)setFileterCollectionView{
    if(!filterCollectionView){
        float viewW =collectionFrame.size.width;
        float viewH =collectionFrame.size.height;
        NSLog(@"w = %f, h = %f",viewW,viewH);
        //    float Wall=kGTSDeviceFullWidth - 40;
        float w = (viewW-51) / 3;
        float h = 35;
        
        UICollectionViewFlowLayout * flow =[[UICollectionViewFlowLayout alloc]init];
        flow.itemSize=CGSizeMake(w, h);
//        flow.headerReferenceSize = CGSizeMake(SCREEN_WIDTH,40.0f);
        flow.footerReferenceSize = CGSizeMake(SCREEN_WIDTH,10.0f);
        flow.sectionInset=UIEdgeInsetsMake(15,15,0,15);
        filterCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, viewW, viewH-50) collectionViewLayout:flow];
        [filterCollectionView registerClass:[STTimerDayCell class] forCellWithReuseIdentifier:@"STTimerDayCell"];
        [filterCollectionView registerClass:[UICollectionReusableView class]
                 forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                        withReuseIdentifier:@"footerView"];
//        [filterCollectionView registerClass:[UICollectionReusableView class]
//                 forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
//                        withReuseIdentifier:@"headerView"];
        filterCollectionView.delegate = self;
        filterCollectionView.dataSource = self;
        //    filterCollectionView.scrollEnabled=NO;
        filterCollectionView.showsHorizontalScrollIndicator = NO;
        filterCollectionView.showsVerticalScrollIndicator = NO;
        filterCollectionView.backgroundColor = [UIColor clearColor];
        [self addSubview:filterCollectionView];
        
        [self loadFilterBtn];
    }else{
        [filterCollectionView reloadData];
    }
    
    
}

-(void)loadFilterBtn{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-50, SCREEN_WIDTH, 50)];
    UIButton * replayBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-(75+15)*2, (50-35)/2, 75, 35)];
    [replayBtn setTitle:@"重制" forState:UIControlStateNormal];
    [replayBtn setTitleColor:ButtonBackColor forState:UIControlStateNormal];
    replayBtn.layer.borderColor = ButtonBackColor.CGColor;
    replayBtn.layer.borderWidth = 1;
    [replayBtn addTarget:self action:@selector(actionFilterReplay) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-(75+15)*1, (50-35)/2, 75, 35)];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.backgroundColor = ButtonBackColor;
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(actionFilterSure) forControlEvents:UIControlEventTouchUpInside];
    
    replayBtn.layer.cornerRadius = 5;
    replayBtn.layer.masksToBounds = YES;
    sureBtn.layer.cornerRadius = 5;
    sureBtn.layer.masksToBounds = YES;
    
    [view addSubview:replayBtn];
    [view addSubview:sureBtn];
    [self addSubview:view];
}

#pragma mark -collectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return arrRows.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *selfreusableview = nil;
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *selfFooterView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
        selfreusableview = selfFooterView;
       
    }
    [selfreusableview removeAllSubviews];
    UIView * lineView = [[UILabel alloc]initWithFrame:CGRectMake(15, 9.5, SCREEN_WIDTH-30, 0.5)];
    lineView.backgroundColor = [UIColor grayColor];
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headView.backgroundColor = [UIColor clearColor];
    [headView addSubview:lineView];
    [selfreusableview addSubview:headView];

    return selfreusableview;
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

//
//// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *identifier = @"STTimerDayCell";
    NSString *identifier=[NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
    [collectionView registerClass:[STTimerDayCell class] forCellWithReuseIdentifier:identifier];
    STTimerDayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    SWServiceModel * model = arrRows[indexPath.row];
    [cell setOwnCollectionCellWithData:model];
    [cell.dayBtn setBackgroundImage:nil forState:UIControlStateNormal];
    cell.dayBtn.backgroundColor = [UIColor whiteColor];
    [cell.dayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cell.isSel = NO;
    cell.dayBtn.enabled = NO;
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    STTimerDayCell * cell = (STTimerDayCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSIndexPath * newIndexPath ;
    cell.isSel = !cell.isSel;
    
    SWServiceModel * model = arrRows[indexPath.row];
    
    if (indexPath.section==0) {
        if (cell.isSel) {
            gradeName = cell.dayBtn.titleLabel.text;
        }else{
            gradeName = nil;
        }
        itemID = model.ID;
    }else if (indexPath.section==1) {
        if (cell.isSel) {
            activeName = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        }else{
            activeName = nil;
        }
    }
    if (!cell.isSel) {
        newIndexPath = [NSIndexPath indexPathForRow:100 inSection:indexPath.section];
    }else{
        newIndexPath = indexPath;
    }
    [self setSectionOtherCell:newIndexPath];
   
   
}

//- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section == oldIndexPath.section) {
//        STTimerDayCell *cell = (STTimerDayCell *)[collectionView cellForItemAtIndexPath:indexPath];
//        [self didselectCell:cell sel:NO];
//    }
//}
-(void)setSectionOtherCell:(NSIndexPath *)indexPath{
    for (int i = 0; i<arrRows.count; i++) {
        NSIndexPath * newIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
        STTimerDayCell * cell = (STTimerDayCell *)[filterCollectionView cellForItemAtIndexPath:newIndexPath];
        if (i == indexPath.row) {
            [self didselectCell:cell sel:YES];
        }else{
            [self didselectCell:cell sel:NO];
        }
    }
}

-(void)didselectCell:(STTimerDayCell *)cell sel:(BOOL)sel{
    if (sel) {
        cell.dayBtn.backgroundColor = [UIColor orangeColor];
        [cell.dayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        cell.dayBtn.backgroundColor = [UIColor whiteColor];
        [cell.dayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
}

//-(void)actionSelWenkDay:(UIButton *)sender{
//    sender.selected =!sender.selected;
//    STTimerDayCell * cell =  (STTimerDayCell *)sender.superview.superview;
//    NSIndexPath * indexPath =[filterCollectionView indexPathForCell:cell];
//    NSDictionary * dicRow = arrRows[indexPath.section];
//    NSArray * arrData = dicRow[@"data"];
//    if (sender.selected) {
//        [arrSel addObject:arrData[indexPath.row]];
//    }else{
//        [arrSel removeObject:arrData[indexPath.row]];
//    }
//}

-(void)actionFilterReplay{
    gradeName = nil;
    activeName = nil;
    [filterCollectionView reloadData];
    
}

-(void)actionFilterSure{
    if([self.delegate respondsToSelector:@selector(sureFilterCondition:)]){
        NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:2];
        [dic setValue:gradeName forKey:@"grade"];
        [dic setValue:activeName forKey:@"active"];
        [dic setValue:[NSNumber numberWithInteger:itemID] forKey:@"itemid"];
        [self.delegate sureFilterCondition:dic];
    }
}


@end
